<?php
declare (strict_types=1);

namespace app\mall\model\product\admin;

use app\common\model\Attach;
use app\common\model\BaseModel;
use app\common\model\category\Category;
use app\mall\event\subscribes\Product as ProductSubscribe;
use think\facade\Event;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;

/**
 * 定义机构数据模型
 */
class Product extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'mall_product';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;
    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'               => 'integer',
        'status'           => 'integer',
        'fullcategorypath' => 'array',
    ];

    protected $globalScope = ['mhm'];

    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

    public function coverUrl(): BelongsTo
    {
        return $this->belongsTo(Attach::class, 'cover')->bind(['cover_url' => 'fileurl']);
    }

    public function getCategoryAttr($val): string
    {
        $ca = app(Category::class)->setCategoryType(6)->whereIn('id', explode(',', trim($val, ',')))->column('title');
        return implode('/', $ca);
    }

    /**
     * 简介格式转换,防止富文本编辑器中有表情等
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-21
     *
     * @param string $value [description]
     */
    public function setInfoAttr(string $value = ''): string
    {
        return base64_encode($value);
    }

    /**
     * 简介格式转换,防止富文本编辑器中有表情等
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-21
     *
     * @param string $value [description]
     */
    public function getInfoAttr(string $value = '')
    {
        if (!$value) {
            return '';
        }
        if ($value === base64_encode((string)base64_decode($value, true))) {
            return base64_decode($value);
        } else {
            return $value;
        }
    }

    /**
     * 关联轮播
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/29
     *
     * @return HasMany
     */
    public function swiperList(): HasMany
    {
        return $this->hasMany(ProductSwiper::class, 'product_id')->with(['attach']);
    }

    /**
     * 成功创建之后
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/12/05
     *
     * @static
     * @param $model
     */
    public static function onAfterInsert($model)
    {
        Event::subscribe(ProductSubscribe::class);
        Event::trigger('AfterCreate', $model);
    }

    /**
     * 成功修改之后
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/12/05
     *
     * @static
     * @param $model
     */
    public static function onAfterUpdate($model)
    {
        Event::subscribe(ProductSubscribe::class);
        Event::trigger('AfterUpdate', $model);
    }

    /**
     * 成功删除之后
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/12/05
     *
     * @static
     * @param $model
     */
    public static function onAfterDelete($model)
    {
        Event::subscribe(ProductSubscribe::class);
        Event::trigger('AfterDelete', $model);
    }

}
